#include "alarm_rule.h"
#include <QJsonObject>
#include <QJsonDocument>

AlarmRule::AlarmRule(MetricTypeEnum metric_type, AlarmLevelEnum alarm_level,
                     ThresholdUnitEnum threshold_unit, int64_t state_id)
    : metric_type_(metric_type),
      alarm_level_(alarm_level),
      threshold_unit_(threshold_unit),
      is_enabled_(true),
      interval_secs_(0),
      threshold_value_(0),
      state_id_(state_id)
{
}

AlarmRule::~AlarmRule()
{
}

QString AlarmRule::getAlarmLevelText(AlarmLevelEnum level)
{
    switch (level)
    {
    case AlarmLevelEnum::ALERT:
        return "提醒";
    case AlarmLevelEnum::NOTICE:
        return "通知";
    case AlarmLevelEnum::ATTENTION:
        return "关注";
    case AlarmLevelEnum::CRITICAL:
        return "紧急";
    default:
        return "";
    }
}

QString AlarmRule::getAlarmLevelColor(const QString &level)
{
    if (level == "提醒")
        return "#2196F3"; // 蓝色
    else if (level == "通知")
        return "#FFC107"; // 黄色
    else if (level == "关注")
        return "#FF9800"; // 橙色
    else if (level == "紧急")
        return "#F44336"; // 红色
    return "#000000";     // 默认黑色
}

QString AlarmRule::getMetricTypeText(MetricTypeEnum type)
{
    switch (type)
    {
    case MetricTypeEnum::CONTROL_FRAME_TOTAL:
        return "控制帧总数";
    case MetricTypeEnum::CONTROL_POINT_TOTAL:
        return "控制点总数";
    case MetricTypeEnum::SAME_YK_POINT:
        return "多次遥控相同地址";
    case MetricTypeEnum::SAME_SD_POINT:
        return "多次设值相同地址";
    case MetricTypeEnum::YT_PRECISION:
        return "设值的目标值与遥测值相差";
    case MetricTypeEnum::YC_SLOPE:
        return "遥测值跳变超过";
    default:
        return "";
    }
}

AlarmLevelEnum AlarmRule::getAlarmLevelFromText(const QString &text)
{
    if (text == "提醒")
        return AlarmLevelEnum::ALERT;
    else if (text == "通知")
        return AlarmLevelEnum::NOTICE;
    else if (text == "关注")
        return AlarmLevelEnum::ATTENTION;
    else if (text == "紧急")
        return AlarmLevelEnum::CRITICAL;
    return AlarmLevelEnum::NOTICE; // 默认通知级别
}

MetricTypeEnum AlarmRule::metricType() const
{
    return metric_type_;
}

AlarmLevelEnum AlarmRule::alarmLevel() const
{
    return alarm_level_;
}

ThresholdUnitEnum AlarmRule::thresholdUnit() const
{
    return threshold_unit_;
}

int AlarmRule::intervalSecs() const
{
    return interval_secs_;
}

void AlarmRule::setIntervalSecs(int secs)
{
    interval_secs_ = secs;
}

double AlarmRule::thresholdValue() const
{
    return threshold_value_;
}

void AlarmRule::setThresholdValue(double value)
{
    threshold_value_ = value;
}

bool AlarmRule::isEnabled() const
{
    return is_enabled_;
}

void AlarmRule::setEnabled(bool enabled)
{
    is_enabled_ = enabled;
}

int AlarmRule::stateId() const
{
    return state_id_;
}

void AlarmRule::setStateId(int state_id)
{
    state_id_ = state_id;
}

QString AlarmRule::toJson() const
{
    QJsonObject json;
    json["metric_type"] = metric_type_;
    json["alarm_level"] = alarm_level_;
    json["threshold_unit"] = threshold_unit_;
    json["is_enabled"] = is_enabled_;
    json["interval_secs"] = interval_secs_;
    json["threshold_value"] = threshold_value_;
    json["state_id"] = static_cast<qint64>(state_id_);
    return QJsonDocument(json).toJson(QJsonDocument::Compact);
}

QString AlarmRule::toRichText() const
{
    QString levelText = getAlarmLevelText(alarm_level_);
    QString levelColor = getAlarmLevelColor(levelText);
    QString metricText = getMetricTypeText(metric_type_);
    
    QString thresholdStr;
    if (threshold_unit_ == PERCENT) {
        thresholdStr = QString::number(threshold_value_, 'f', 2) + "%";
    } else {
        thresholdStr = QString::number(threshold_value_);
    }
    
    return QString("在 <span style='font-weight: bold;'>%1</span> 秒内，"
                  "<span style='font-weight: bold;'>%2</span> 超 "
                  "<span style='font-weight: bold;'>%3</span>，触发"
                  " <span style='font-weight: bold; color: %4;'>%5</span> 告警")
            .arg(interval_secs_)
            .arg(metricText)
            .arg(thresholdStr)
            .arg(levelColor)
            .arg(levelText);
}

QString AlarmRule::toPlainText() const
{
    QString levelText = getAlarmLevelText(alarm_level_);
    QString metricText = getMetricTypeText(metric_type_);
    QString thresholdStr;
    if (threshold_unit_ == PERCENT) {
        thresholdStr = QString::number(threshold_value_, 'f', 2) + "%";
    } else {
        thresholdStr = QString::number(threshold_value_);
    }
    return QString("在 %1 秒内，%2 超 %3，触发 %4 告警")
        .arg(interval_secs_)
        .arg(metricText)
        .arg(thresholdStr)
        .arg(levelText);
}

void AlarmRule::setId(int id)
{
    id_ = id;
}

int AlarmRule::id() const
{
    return id_;
}
